/*
 * MimeToolApp.java
 */
package se.jtech.jdf.mimetool;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class MimeToolApp extends SingleFrameApplication {

    private MimeToolView view;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        if (System.getProperty("os.name").startsWith("Mac")) {
            configureForMacOSX();
        }
        view = new MimeToolView(this, getEncoding());
        show(view);
    }

    @Override
    protected void shutdown() {
        setEncoding(view.getEncoding());
        super.shutdown();
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of MimeToolApp
     */
    public static MimeToolApp getApplication() {
        return Application.getInstance(MimeToolApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(MimeToolApp.class, args);
    }

    public Preferences getPreferences() {
        return Preferences.userNodeForPackage(getClass());
    }

    /**
     * @return the user's encoding setting
     */
    public Encoding getEncoding() {
        return Encoding.valueOf(getPreferences().get(PreferencesKeys.Encoding.name(), Encoding.Base64.name()));
    }

    /**
     * Sets the user's encoding setting.
     * @param encoding the encoding
     */
    public void setEncoding(Encoding encoding) {
        getPreferences().put(PreferencesKeys.Encoding.name(), encoding.name());
    }

    public enum Encoding {

        Base64("base64"), Binary("binary"), Bit7("7bit"), Bit8("8bit"), QuotedPrintable("quoted-printable");
        private final String name;

        Encoding(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return getName();
        }
    }

    enum PreferencesKeys {

        Encoding;
    }

    private void configureForMacOSX() {
        Logger.getLogger(MimeToolApp.class.getName()).log(Level.INFO, "Configuring for Mac OS X...");
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        // Reflectively configure for Mac OS X
        try {
            Class applicationClass = Class.forName("com.apple.eawt.Application");
            Method getApplicationMethod = applicationClass.getDeclaredMethod("getApplication", (Class[]) null);
            Object applicationObject = getApplicationMethod.invoke((Object) null, (Object[]) null);
//            Method setDockIconImageMethod = applicationObject.getClass().getDeclaredMethod("setDockIconImage", new Class[]{java.awt.Image.class});
//            setDockIconImageMethod.invoke(applicationObject, new Object[]{getResourceMap().getImageIcon("dock.icon").getImage()});
            Runtime.getRuntime().addShutdownHook(new Thread() {

                @Override
                public void run() {
                    getApplication().shutdown();
                }
            });
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MimeToolApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(MimeToolApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(MimeToolApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(MimeToolApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(MimeToolApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MimeToolApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
